package jou.cs.controller;

import jou.cs.configurator.CONST;
import jou.cs.entity.TPlace;
import jou.cs.mapper.PlaceRepository;
import jou.cs.service.TrendService;
import jou.cs.utils.UserSet;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("trend")
public class trend {
    @Resource
    private TrendService trendService;
    @Resource
    PlaceRepository repository;

    @GetMapping("{page}")
    public Object f1(@PathVariable Integer page, @RequestParam String uid) {
        // 用户id进行推荐
        List<UserSet.Set> trend = trendService.getTrend(uid);

        //repository page
        List<Long> ids = new ArrayList<>();

        List<TPlace> collect = new ArrayList<>();
        for (UserSet.Set set : trend) {
            String type = set.username;

            collect.addAll(repository.findAll().stream()
                    .filter(p -> Arrays.asList(p.getType().split(","))
                    .contains(type)).collect(Collectors.toList()));
        }

        List<TPlace> ls = collect.stream().distinct().collect(Collectors.toList());

        if (ls.size() < 2) {
            ls.addAll(repository.findAll(PageRequest.of(page - 1, CONST.PAGE_SIZE)).getContent());
        }

        return ls;
    }

    @PostMapping("submit")
    public Object f2(@RequestParam Long uid, @RequestParam Long pid, @RequestParam String type) {
        // 记录用户行为
        trendService.record(uid, pid, type);
        return null;
    }
}
